package com.mall.service.mallLogs.mapper;

import com.mall.model.domain.MallLogs;
import com.mall.model.dto.MallLogsDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MallLogsMapper {

    @Insert("INSERT INTO `malllog` (\n" +
            "  userId,\n" +
            "  currentTime,\n" +
            "  methodUrl,\n" +
            "  method,\n" +
            "  `time`,\n" +
            "  isTimeout\n" +
            ")\n" +
            "VALUES\n" +
            "  (\n" +
            "    #{userId},\n" +
            "    #{currentTime},\n" +
            "    #{methodUrl},\n" +
            "    #{method},\n" +
            "    #{time},\n" +
            "    #{isTimeout}\n" +
            "  )")
    void save(MallLogs mallLogs);

    //统计接口超时次数
    //sql语句：先查出昨日一天的全部超时接口，以此表为基础，再查询出相同接口超时的次数
    @Select("SELECT\n" +
            "  methodUrl,\n" +
            "  method,\n" +
            "  isTimeout,\n" +
            "  COUNT(*) AS `countTimeout`\n" +
            "FROM\n" +
            "  (SELECT\n" +
            "    *\n" +
            "  FROM\n" +
            "    mallLog\n" +
            "  WHERE currentTime > #{startTime}\n" +
            "    AND currentTime < #{endTime}) AS a\n" +
            "GROUP BY methodUrl,\n" +
            "  method,\n" +
            "  isTimeout\n" +
            "HAVING isTimeout = 1")
    List<MallLogsDto> countTimeout(String startTime, String endTime);
}
